import Vue from 'vue';
//导入路由
import VueRouter from 'vue-router';

//引入组件
import home from '../src/components/home/home.vue'
import login from '../src/components/login/login.vue'
import power from '../src/components/powers/power.vue'
import roles from '../src/components/roles/roles.vue'
import user from '../src/components/users/user.vue'
//安装一下路由
Vue.use(VueRouter)
const router=new VueRouter({
  routes:[
    {
      //重定向让页面一打开进去那里
      path:'/',redirect:'/login'
    },
    {
      path:"/login",component:login
    },
    {
      path:"/home",component:home,children:[
        {
          path:'/user',component:user
        },
        {
          path:'/power',component:power
        },
        {
          path:'/roles',component:roles
        }
      ]
    }
  ]
})
//路由守卫
router.beforeEach((to,from,next)=>{
      if(to.path ==='/login')
      {
            next()
      }else{
        const token=localStorage.getItem('token')
        token ? next() : next('/login')
      }
    })





//导出路由
export default router
